/*
 * @Description:
 * @Version:
 * @Author: Troubadour
 * @Date: 2023-01-18 14:31:37
 * @LastEditors: Troubadour 2276791354@qq.com
 * @LastEditTime: 2023-11-04 16:30:30
 */

#include "display.h"
#include <math.h>





void Display_Clock(void)
{
    uint16_t color = BLUE;
    uint8_t clockCenterPoint = 119;
    uint8_t clockRadius = 100;
    /* 外圆 */
    TFT_GUI_DrawCircle(clockCenterPoint, clockCenterPoint, clockRadius - 1, color);
    TFT_GUI_DrawCircle(clockCenterPoint, clockCenterPoint, clockRadius,     color);
    TFT_GUI_DrawCircle(clockCenterPoint, clockCenterPoint, clockRadius + 1, color);
    TFT_GUI_DrawCircle(clockCenterPoint, clockCenterPoint, clockRadius + 2, color);
    /* 刻度 0*/
    TFT_GUI_DrawLine(clockCenterPoint - 1, clockCenterPoint - clockRadius, clockCenterPoint - 1, clockCenterPoint - clockRadius + 20, color);
    TFT_GUI_DrawLine(clockCenterPoint,     clockCenterPoint - clockRadius, clockCenterPoint,     clockCenterPoint - clockRadius + 20, color);
    TFT_GUI_DrawLine(clockCenterPoint + 1, clockCenterPoint - clockRadius, clockCenterPoint + 1, clockCenterPoint - clockRadius + 20, color);
    /* 刻度 1*/
    TFT_GUI_DrawLine(159, 47, 167, 35, color);
    TFT_GUI_DrawLine(160, 48, 168, 36, color);
    TFT_GUI_DrawLine(161, 49, 169, 37, color);
    /* 刻度 2*/
    TFT_GUI_DrawLine(190, 78, 202, 70, color);
    TFT_GUI_DrawLine(191, 79, 203, 71, color);
    TFT_GUI_DrawLine(192, 80, 204, 72, color);
    /* 刻度 3*/
    TFT_GUI_DrawLine(201, 118, 217, 118, color);
    TFT_GUI_DrawLine(201, 119, 217, 119, color);
    TFT_GUI_DrawLine(201, 120, 217, 120, color);
    /* 刻度 4*/
    TFT_GUI_DrawLine(189, 160, 204, 172, color);
    TFT_GUI_DrawLine(190, 159, 205, 171, color);
    TFT_GUI_DrawLine(191, 158, 206, 170, color);
    /* 刻度 5*/
    TFT_GUI_DrawLine(159, 190, 168, 206, color);
    TFT_GUI_DrawLine(160, 189, 169, 205, color);
    TFT_GUI_DrawLine(161, 188, 170, 204, color);
    /* 刻度 6*/
    TFT_GUI_DrawLine(clockCenterPoint - 1, clockCenterPoint + clockRadius, clockCenterPoint - 1, clockCenterPoint + clockRadius - 20, color);
    TFT_GUI_DrawLine(clockCenterPoint,     clockCenterPoint + clockRadius, clockCenterPoint,     clockCenterPoint + clockRadius - 20, color);
    TFT_GUI_DrawLine(clockCenterPoint + 1, clockCenterPoint + clockRadius, clockCenterPoint + 1, clockCenterPoint + clockRadius - 20, color);
    /* 刻度 7*/
    TFT_GUI_DrawLine(80, 189, 69, 203, color);
    TFT_GUI_DrawLine(81, 190, 70, 204, color);
    TFT_GUI_DrawLine(82, 191, 71, 205, color);
    /* 刻度 8*/
    TFT_GUI_DrawLine(49, 158, 34, 168, color);
    TFT_GUI_DrawLine(50, 159, 35, 169, color);
    TFT_GUI_DrawLine(51, 160, 36, 170, color);
    /* 刻度 9*/
    TFT_GUI_DrawLine(19, 118, 40, 118, color);
    TFT_GUI_DrawLine(19, 119, 40, 119, color);
    TFT_GUI_DrawLine(19, 120, 40, 120, color);
    /* 刻度 10*/
    TFT_GUI_DrawLine(49, 81, 34, 71, color);
    TFT_GUI_DrawLine(50, 80, 35, 70, color);
    TFT_GUI_DrawLine(51, 79, 36, 69, color);
    /* 刻度 11*/
    TFT_GUI_DrawLine(80, 51, 70, 36, color);
    TFT_GUI_DrawLine(81, 50, 71, 35, color);
    TFT_GUI_DrawLine(82, 49, 72, 34, color);
}


#define HOUR_COLOR          BROWN
#define MINUTE_COLOR        BLUE
#define SECOND_COLOR        RED
#define HOUR_HAND_LEN       (50)
#define MINUTE_HAND_LEN     (60)
#define SECOND_HAND_LEN     (70)
#define CLOCK_X             (119)
#define CLOCK_Y             (119)


void Display_ClockHand(uint8_t hour, uint8_t minute, uint8_t second)
{
    static int LastHourX = 0, LastHourY = 0;
    static int LastMinuteX = 0, LastMinuteY = 0;
    static int LastSecondX = 0, LastSecondY = 0;
    static uint8_t LastMinute = 0;

    float Angle = 0;
    int x0, y0;
    double k;

    hour = hour % 24;
    minute = minute % 60;
    second = second % 60;

    /* 清除上次的时针 */
    if (LastMinute != minute)
    {
        if (LastHourX == 0 && LastHourY == 0)
        {
            k = (360 / 12 * 11) * (3.1415926535 / 180);
            x0 = sin(k) * HOUR_HAND_LEN;
            y0 = cos(k) * HOUR_HAND_LEN;
            TFT_GUI_DrawLine(CLOCK_X, CLOCK_Y, CLOCK_X + x0, CLOCK_Y - y0, BLACK);
            TFT_GUI_DrawLine(CLOCK_X - 1, CLOCK_Y - 1, CLOCK_X + x0 - 1, CLOCK_Y - y0, BLACK);
            TFT_GUI_DrawLine(CLOCK_X + 1, CLOCK_Y + 1, CLOCK_X + x0 + 1, CLOCK_Y - y0, BLACK);
        }
        else
        {
            TFT_GUI_DrawLine(CLOCK_X, CLOCK_Y, LastHourX, LastHourY, BLACK);
            TFT_GUI_DrawLine(CLOCK_X - 1, CLOCK_Y - 1, LastHourX - 1, LastHourY, BLACK);
            TFT_GUI_DrawLine(CLOCK_X + 1, CLOCK_Y + 1, LastHourX + 1, LastHourY, BLACK);
        }
        /* 清除上次的分针 */
        if (minute == 0)
        {
            k = (360 / 60 * 59) * (3.1415926535 / 180);
            x0 = sin(k) * MINUTE_HAND_LEN;
            y0 = cos(k) * MINUTE_HAND_LEN;
            TFT_GUI_DrawLine(CLOCK_X, CLOCK_Y, CLOCK_X + x0, CLOCK_Y - y0, BLACK);
            TFT_GUI_DrawLine(CLOCK_X - 1, CLOCK_Y - 1, CLOCK_X + x0 - 1, CLOCK_Y - y0, BLACK);
            TFT_GUI_DrawLine(CLOCK_X + 1, CLOCK_Y + 1, CLOCK_X + x0 + 1, CLOCK_Y - y0, BLACK);
        }
        else
        {
            TFT_GUI_DrawLine(CLOCK_X, CLOCK_Y, LastMinuteX, LastMinuteY, BLACK);
            TFT_GUI_DrawLine(CLOCK_X - 1, CLOCK_Y - 1, LastMinuteX - 1, LastMinuteY, BLACK);
            TFT_GUI_DrawLine(CLOCK_X + 1, CLOCK_Y + 1, LastMinuteX + 1, LastMinuteY, BLACK);
        }
    }
    /* 清除上次的秒针 */
    if (second == 0)
    {
        k = (360 / 60 * 59) * (3.1415926535 / 180);
        x0 = sin(k) * SECOND_HAND_LEN;
        y0 = cos(k) * SECOND_HAND_LEN;
        TFT_GUI_DrawLine(CLOCK_X, CLOCK_Y, CLOCK_X + x0, CLOCK_X - y0, BLACK);
    }
    else
    {
        TFT_GUI_DrawLine(CLOCK_X, CLOCK_Y, LastSecondX, LastSecondY, BLACK);
    }

    /* 画秒针 */
    Angle = 360 / 60 * second;
    k = Angle * (3.1415926535 / 180);
    x0 = sin(k) * SECOND_HAND_LEN;
    y0 = cos(k) * SECOND_HAND_LEN;
    TFT_GUI_DrawLine(CLOCK_X, CLOCK_Y, CLOCK_X + x0, CLOCK_Y - y0, SECOND_COLOR);
    TFT_ShowNum(210, 239-16, second++, 2, SECOND_COLOR);
    LastSecondX = CLOCK_X + x0;
    LastSecondY = CLOCK_Y - y0;
    /* 画分针 */
    Angle = 360 / 60 * minute;
    k = Angle * (3.1415926535 / 180);
    x0 = sin(k) * MINUTE_HAND_LEN;
    y0 = cos(k) * MINUTE_HAND_LEN;
    TFT_GUI_DrawLine(CLOCK_X, CLOCK_Y, CLOCK_X + x0, CLOCK_Y - y0, MINUTE_COLOR);
    TFT_GUI_DrawLine(CLOCK_X - 1, CLOCK_Y - 1, CLOCK_X + x0 - 1, CLOCK_Y - y0, MINUTE_COLOR);
    TFT_GUI_DrawLine(CLOCK_X + 1, CLOCK_Y + 1, CLOCK_X + x0 + 1, CLOCK_Y - y0, MINUTE_COLOR);
    TFT_ShowNum(180, 239 - 16, minute, 2, MINUTE_COLOR);
    LastMinuteX = CLOCK_X + x0;
    LastMinuteY = CLOCK_Y - y0;
    /* 画时针 */
    Angle = 360.0 / 12.0 * (hour + (minute / 60.0));
    k = Angle * (3.1415926535 / 180);
    x0 = sin(k) * HOUR_HAND_LEN;
    y0 = cos(k) * HOUR_HAND_LEN;
    TFT_GUI_DrawLine(CLOCK_X, CLOCK_Y, CLOCK_X + x0, CLOCK_Y - y0, HOUR_COLOR);
    TFT_GUI_DrawLine(CLOCK_X - 1, CLOCK_Y - 1, CLOCK_X + x0 - 1, CLOCK_Y - y0, HOUR_COLOR);
    TFT_GUI_DrawLine(CLOCK_X + 1, CLOCK_Y + 1, CLOCK_X + x0 + 1, CLOCK_Y - y0, HOUR_COLOR);
    TFT_ShowNum(150, 239 - 16, hour, 2, HOUR_COLOR);
    LastHourX = CLOCK_X + x0;
    LastHourY = CLOCK_Y - y0;
}

extern const unsigned char FontChar64x32[];
void Display_Temp(float temp)
{
    if (1)
    {
        printf("test: %d \r\n", FontChar64x32[0]);
    }
}



